/**
 * 
 */
package org.going.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.going.invoker.WebFunction;
import org.going.utils.GoingConst;

/**
 * @author ferendo.huang@gmail.com
 * 
 */
@SuppressWarnings("serial")
@WebServlet(asyncSupported = false, urlPatterns = { GoingConst.GOING_WEBOBJ_URL })
public class WebObjectServlet extends HttpServlet implements GoingConst
{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		String[] items = req.getRequestURI().split(URL_SEP);
		String beanMethod = items[items.length - 1];
		@SuppressWarnings("unchecked")
		Map<String, WebFunction> functionMap = (Map<String, WebFunction>) req.getServletContext().getAttribute(
				WEB_FUNCTION_MAP);
		WebFunction function = functionMap.get(beanMethod);
		if (function == null)
			throw new ServletException("No such bean Method defined, beanMethod : " + beanMethod);
		try
		{
			function.invoke(req, resp);
		}
		catch (Exception e)
		{
			throw new ServletException("bean Method invoke fail, beanMethod : " + beanMethod, e);
		}
	}

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
	}

}
